﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DALUser : IDAL.IDALUser
    {

        #region 添加用户
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<User> Add(User user)
        {
            //url
            string url = "https://localhost:44397/Api/Test/Post";
            //将对象序列化为json字符串
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
            //使用HttpClient发送POST请求
            using (var client = new HttpClient())
            {
                //设置请求头
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                //发送POST请求
                var result = client.PostAsync(url, content).Result;
                //判断请求是否成功
                if (result.IsSuccessStatusCode)
                {
                    //读取响应内容
                    var responseContent = result.Content.ReadAsStringAsync().Result;
                    //将响应内容反序列化为对象
                    var responseUser = Newtonsoft.Json.JsonConvert.DeserializeObject<Structure>(responseContent);
                    //判断是否添加成功
                    if (responseUser.code == "200")
                    {
                        return responseUser.data;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

        #region 删除用户
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public List<User> Delete(int id)
        {
          string url = "https://localhost:44397/Api/Test/Delete/" + id;
            //使用HttpClient发送DELETE请求
            using (var client = new HttpClient())
            {
                //发送DELETE请求
                var result = client.DeleteAsync(url).Result;
                //判断请求是否成功
                if (result.IsSuccessStatusCode)
                {
                    //读取响应内容
                    var responseContent = result.Content.ReadAsStringAsync().Result;
                    //将响应内容反序列化为对象
                    var responseUser = Newtonsoft.Json.JsonConvert.DeserializeObject<Structure>(responseContent);
                    //判断是否删除成功
                    if (responseUser.code =="200")
                    {
                        return responseUser.data;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

        #region 查询用户
        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<User> Inquire()
        {
            string url = "https://localhost:44397/Api/Test/Get";
            //使用HttpClient发送GET请求
            using (var client = new HttpClient())
            {
                //发送GET请求
                var result = client.GetAsync(url).Result;
                //判断请求是否成功
                if (result.IsSuccessStatusCode)
                {
                    //读取响应内容
                    var responseContent = result.Content.ReadAsStringAsync().Result;
                    //将响应内容反序列化为对象
                    var users = Newtonsoft.Json.JsonConvert.DeserializeObject<Structure>(responseContent);
                    return users.data;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 查询指定ID的用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<User> Inquire(int id)
        {
          string url = "https://localhost:44397/Api/Test/Get/" + id;
            //使用HttpClient发送GET请求
            using (var client = new HttpClient())
            {
                //发送GET请求
                var result = client.GetAsync(url).Result;
                //判断请求是否成功
                if (result.IsSuccessStatusCode)
                {
                    //读取响应内容
                    var responseContent = result.Content.ReadAsStringAsync().Result;
                    //将响应内容反序列化为对象
                    var user = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(responseContent);
                    //判断是否查询成功
                    if (user != null)
                    {
                        List<User> users = new List<User>();
                        users.Add(user);
                        return users;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

        #region 更新用户
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<User> Update(User user)
        {
            string url = $"https://localhost:44397/Api/Test/Put/{user.Id}";
            //将对象序列化为json字符串
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
            //使用HttpClient发送PUT请求
            using (var client = new HttpClient())
            {
                //设置请求头
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                //发送PUT请求
                var result = client.PutAsync(url, content).Result;
                //判断请求是否成功
                if (result.IsSuccessStatusCode)
                {
                    
                    //读取响应内容
                    var responseContent = result.Content.ReadAsStringAsync().Result;
                    //将响应内容反序列化为对象
                    var responseUser = Newtonsoft.Json.JsonConvert.DeserializeObject<Structure>(responseContent);
                    //判断是否更新成功
                    if (responseUser.code=="200")
                    {
                        return responseUser.data;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

    }
}
